<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        :inline="true"
        label-width="80px"
        @keyup.enter.native="reload"
        @submit.native.prevent
      >
        <el-form-item label="筛选日期:">
          <el-date-picker
            style="width: 100%"
            v-model="where.time_range"
            type="datetimerange"
            value-format="yyyy-MM-dd HH:mm:ss"
            label-format="yyyy-MM-dd HH:mm:ss"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item label="CDK批次">
          <PageSelect
            :placeholder="'请选择激活码批次'"
            v-model="where.batch_id"
            :whereName="'name'"
            :url="'/BoxYunCdkBatch/index'"
            :optionLable="'name'"
            :optionvalue="'id'"
            :multiple="false"
          />
        </el-form-item>

        <el-form-item label="激活码:">
          <el-input clearable v-model="where.cdk" placeholder="请输入激活码" />
        </el-form-item>

        <el-form-item label="激活状态:">
          <el-select
            clearable
            v-model="where.is_active"
            placeholder="请选择激活状态"
            class="ele-fluid"
          >
            <el-option label="未激活" value="0" />
            <el-option label="已激活" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="启用状态:">
          <el-select
            clearable
            v-model="where.status"
            placeholder="请选择启用状态"
            class="ele-fluid"
          >
            <el-option label="全部" value="" />
            <el-option label="已启用" value="1" />
            <el-option label="未启用" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item action="">
          <div class="ele-form-actions">
            <el-button
              type="primary"
              icon="el-icon-search"
              class="ele-btn-icon"
              @click="reload"
              >查询
            </el-button>
            <el-button @click="reset">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        height="calc(100vh - 315px)"
      >
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit(null)"
            v-if="permission.includes('sys:box-yun-cdk-cdk:add')"
            >新建激活码
          </el-button>
          <el-button
            size="small"
            type="success"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="startBatch(1)"
            v-if="permission.includes('sys:box-yun-cdk-cdk:start')"
            >批量启用
          </el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="startBatch(2)"
            v-if="permission.includes('sys:box-yun-cdk-cdk:end')"
            >批量禁用
          </el-button>
          <el-button
            size="small"
            type="info"
            icon="el-icon-download"
            class="ele-btn-icon"
            @click="exportExcel"
            v-if="permission.includes('sys:box-yun-cdk-cdk:add')"
            >导出激活码
          </el-button>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{ row }">
          <!-- <el-link type="primary" :underline="false" icon="el-icon-edit" @click="openEdit(row)"
            v-if="permission.includes('sys:box-yun-cdk-cdk:edit')">修改
          </el-link> -->
          <el-popconfirm
            class="ele-action"
            title="确定要删除此消息吗？"
            @confirm="remove(row)"
          >
            <el-link
              type="danger"
              slot="reference"
              :underline="false"
              icon="el-icon-delete"
              v-if="permission.includes('sys:box-yun-cdk-cdk:delete')"
              >删除
            </el-link>
          </el-popconfirm>
        </template>
        <!-- 搜索场景 -->
        <template slot="status" slot-scope="{ row }">
          <el-tag v-if="row.status === 1" type="success" size="small"
            >上架</el-tag
          >
          <el-tag v-if="row.status === 2" type="error" size="small"
            >下架</el-tag
          >
        </template>
        <!-- 状态列 -->
        <template slot="status" slot-scope="{ row }">
          <!-- <el-switch v-model="row.status" @change="editStatus(row)" :active-value="1" :inactive-value="2" /> -->
          <el-tag v-if="row.status === 1" type="success" size="small"
            >启用</el-tag
          >
          <el-tag v-if="row.status === 2" type="danger" size="small"
            >禁用</el-tag
          >
        </template>

        <!-- 激活状态 -->
        <template slot="is_active" slot-scope="{ row }">
          <el-tag v-if="row.is_active === 0" type="success" size="small"
            >未激活</el-tag
          >
          <el-tag v-if="row.is_active === 1" type="danger" size="small"
            >已激活</el-tag
          >
        </template>

        <template slot="use_type" slot-scope="{ row }">
          <el-tag v-if="row.use_type === 0" type="success" size="small"
            >不限制</el-tag
          >
          <el-tag v-if="row.use_type === 1" type="danger" size="small"
            >仅新增</el-tag
          >
          <el-tag v-if="row.use_type === 2" type="info" size="small"
            >仅续费</el-tag
          >
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <edit :editData="current" :visible.sync="showEdit" @done="reload" />
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import edit from "./edit";
import PageSelect from "@/components/pageSelect/index.vue";
import { downloadExcel } from "@/utils/excel-export";

export default {
  name: "YunGoodsCdkCode",
  components: { edit, PageSelect },
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: "/BoxYunCdkCode/index",
      // 表格列配置
      columns: [
        {
          columnKey: "selection",
          type: "selection",
          minWidth: 45,
          align: "center",
          fixed: "left",
        },
        {
          prop: "id",
          label: "ID",
          minWidth: 100,
          align: "center",
          showOverflowTooltip: true,
          fixed: "left",
        },
        {
          prop: "batch_name",
          label: "批次名称",
          align: "center",
          showOverflowTooltip: true,
          minWidth: 160,
        },
        {
          prop: "cdk",
          label: "CDK码",
          align: "center",
          showOverflowTooltip: true,
          minWidth: 160,
        },
        {
          prop: "duration",
          label: "激活码时长(单位：天)",
          align: "center",
          showOverflowTooltip: true,
          minWidth: 160,
        },
        {
          prop: "start_time",
          label: "开始时间",
          align: "center",
          minWidth: 160,
        },
        {
          prop: "end_time",
          label: "结束",
          align: "center",
          minWidth: 160,
        },
        {
          prop: "status",
          label: "状态",
          minWidth: 160,
          align: "center",
          slot: "status",
        },
        {
          prop: "is_active",
          label: "激活状态",
          minWidth: 160,
          align: "center",
          slot: "is_active",
        },
        {
          prop: "member_id",
          label: "激活用户盒子ID",
          showOverflowTooltip: true,
          minWidth: 160,
          align: "center",
        },
        {
          prop: "user_device_id",
          label: "激活的userDeviceId",
          minWidth: 280,
          align: "center",
          showOverflowTooltip: true,
        },
        {
          prop: "use_time",
          label: "激活时间",
          minWidth: 160,
          align: "center",
        },
        {
          columnKey: "action",
          label: "操作",
          minWidth: 150,
          align: "center",
          resizable: false,
          slot: "action",
          fixed: "right",
        },
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      confirmMsg: "",
    };
  },
  methods: {
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({ where: this.where });
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({ lock: true });
      this.$http
        .post("/BoxYunCdkCode/delete", { id: row.id })
        .then((res) => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error("请至少选择一条数据");
        return;
      }
      this.$confirm("确定要删除选中的商品吗?", "提示", {
        type: "warning",
      })
        .then(() => {
          const loading = this.$loading({ lock: true });
          this.$http
            .post("/BoxYunCdkCode/delete", {
              id: this.selection.map((d) => d.id),
            })
            .then((res) => {
              loading.close();
              if (res.data.code === 0) {
                this.$message.success(res.data.msg);
                this.reload();
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        })
        .catch(() => {});
    },
    /* 批量开启 */
    startBatch(params) {
      if (!this.selection.length) {
        this.$message.error("请至少选择一条数据");
        return;
      }

      if (params == 1) {
        this.confirmMsg = "确定要批量开启CDK吗?";
      } else {
        this.confirmMsg = "确定要批量禁用CDK吗?";
      }
      this.$confirm(this.confirmMsg, "提示", {
        type: "warning",
      })
        .then(() => {
          const loading = this.$loading({ lock: true });
          this.$http
            .post("/BoxYunCdkCode/startBatch", {
              id: this.selection.map((d) => d.id),
              type: params,
            })
            .then((res) => {
              loading.close();
              if (res.data.code === 0) {
                this.$message.success(res.data.msg);
                this.reload();
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        })
        .catch(() => {});
    },
    /* 更改状态 */
    editStatus(row) {
      const loading = this.$loading({ lock: true });
      let params = Object.assign({
        id: row.id,
        status: row.status,
        is_time: true,
      });
      this.$http
        .post("/BoxYunCdkCode/status", params)
        .then((res) => {
          loading.close();
          if (res.data.code === 0) {
            this.$message({ type: "success", message: res.data.msg });
          } else {
            row.status = !row.status ? 1 : 2;
            this.$message.error(res.data.msg);
          }
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    },
    /* 下载数据Excel */
    exportExcel() {
      downloadExcel(`BoxYunCdkCode/export`, this.where);
    },
  },
};
</script>

<style scoped></style>
